/*******************************************************************************
 * 版权：北京润尼尔网络科技有限公司，保留所有版权<br>
 * 版本：v1.0.0<br>
 * 文件：BaseDialog.js<br>
 * 日期：2017年7月14日<br>
 * 作者: 杨顺强<br>
 * 功能：保存当前实验，<br>
 * 
 * 修改记录：<br>
 * 日期 描述 更新功能<br>
 * 
 ******************************************************************************/
window.makeNamespace(window,"Rainier.UI.V2");

Rainier.UI.V2.SaveAsExperimentDialog = Rainier.UI.V2.BaseDialog.extend({

 	NAME : "com.rainier.application.SaveAsExprimentDialog",

 	/**
 	 * @名称   init
 	 * @功能   完成界面组件的初始化工作
 	 * @author 杨顺强
 	 * @data   2017-07-17
 	 * @return {[type]}   [description]
 	 */
 	init : function(expName){
 		var _this = this;
 		var dialogProperties = {
			    title: '另存为...',
			    width: 400,
			    height: 300,
			    closable : false,
			    closed: false,
			    modal: true,
			    bodyCls:"app-dialog-body",
			    buttons:[{
					text:'确定',
					handler:function(){
						_this.onClickConfirm();
					}
				},{
					text:'取消',
					handler:function(){
						_this.onClickCancel();
					}
				}]
		};
		this.expTextBox       = null;
		this.expInfoComponent = null;
		this.activeEditor     = null;
		this.isOkToSave       = false;
		this.experimentName   = expName;
		this._super(dialogProperties);
 	},

 	/**
 	 * @名称   createDialogComponents
 	 * @功能   创建组内部的子组件对象
 	 * @author 杨顺强
 	 * @data   2017-07-17
 	 * @return {[type]}   [description]
 	 */
 	createDialogComponents : function(){
 		var layoutSupport      = new Rainier.UI.V2.UICompositeLayoutSupport(this.dialog,{fit:true,border:false});

 		var layout_n_properties= {region : "north", height : 40, split : false};
 		var textboxLayout 	   = new Rainier.UI.V2.UICompositeLayout(layoutSupport,layout_n_properties);
 		var layout_c_properties= {region : "center",split:false};
 		var expLayout          = new Rainier.UI.V2.UICompositeLayout(layoutSupport,layout_c_properties);
 		
 		var textboxProperties  = {
 				 fit        :true,
		       	 multiline	:false,
		       	 editable	:true,
		       	 readonly	:false,
		       	 border		:true,
		       	 label	    :'实验名称:',
		       	 labelWidth :80,
		       	 labelPosition:'before'
		};
		this.expTextBox        = new Rainier.UI.V2.UICompositeTextBox(textboxLayout,textboxProperties);
 		

 		var comPanel           = new Rainier.UI.V2.UICompositePanel(expLayout,{fit:true,border:false});
 		this.expInfoComponent  = new Rainier.UI.V2.UICompositeTextBox(comPanel,{fit:true,editable:false,readonly:true,multiline:true});

 		this.activeEditor = window.app.getContext().getActiveEditor();
 		if(this.activeEditor){
 			this.expJsonObject = this.activeEditor.doSave();
 			this.expTextBox.setValue(this.experimentName);
 		}
 		this.initComponentBusiness();
 	},

 	/**
 	 * @名称   initComponentBusiness
 	 * @功能   初始化组件内部的事件处理
 	 * @author 杨顺强
 	 * @data   2017-07-17
 	 * @return {[type]}   [description]
 	 */
 	initComponentBusiness : function(){
 		var _this = this;
 		var ExpTextBoxListener = Rainier.UI.V2.UIEventListener.extend({
			NAME : "ExpTextBoxListener",
			
			init : function(){
		        this._super();                  
	        },
		        
			onEvent : function(sEvent,objEventSource,objArgs){
				var newValue = objArgs[0];
				var oldValue = objArgs[1];
				_this.expInfoComponent.getElement().textbox({
		 		 	iconCls:'',
		 		 	value : ''
		 		})
		 		_this.refreshExpInfoComponentContent(newValue);
			}
		});

		this.expTextBox.registerEventListener("onChange",new ExpTextBoxListener());
 	},

 	/**
 	 * @名称   refreshExpInfoComponentContent
 	 * @功能   当修改实验名字时，完成对名字的校验
 	 * @author 杨顺强
 	 * @data   2017-07-17
 	 * @param  {[type]}   fileName [description]
 	 * @return {[type]}            [description]
 	 */
 	refreshExpInfoComponentContent : function(fileName){
 		this.experimentName = fileName;
 		if(!this.experimentName){
 			this.expInfoComponent.getElement().textbox({
 				iconCls:'icon-no', 
			    iconAlign:'right'
 			})
 			this.expInfoComponent.setValue('请填写实验名称');
 			this.experimentName = '';
 			return false;
 		}

 	},

 	/**
 	 * @名称   beforeConfirmHandler
 	 * @功能   d点击确定时完成对实验的保存
 	 * @author 杨顺强
 	 * @data   2017-07-17
 	 * @return {[type]}   [description]
 	 */
 	beforeConfirmHandler : function(){
 		var _this = this;
 		if(this.activeEditor){
 			var name = this.expTextBox.getValue();
 			if(name){
	 			var json = this.activeEditor.doSaveAs(name);
				var jsonString = JSON.stringify(json,null,4);
				var file = new File([jsonString],name+".ocj", {type: "text/plain;charset=utf-8"});
				
				saveAs(file);
				this.isOkToSave = true;	
 			}
 		}
 	},


 	/**
 	 * @名称   onClickConfirm
 	 * @功能   点击确定按钮，完成实验的保存功能
 	 * @author 杨顺强
 	 * @data   2017-07-17
 	 * @return {[type]}   [description]
 	 */
	onClickConfirm : function(){
		this.beforeConfirmHandler();
		if(this.isOkToSave){
			this.dialog.close();
		}
	},


	/**
	 * @名称   onClickCancel
	 * @功能   关闭另存为对话框
	 * @author 杨顺强
	 * @data   2017-07-17
	 * @return {[type]}   [description]
	 */
	onClickCancel : function(){
		this.dialog.close();
	},

 });